package org.asiainfo.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.asiainfo.common.log.annotation.Log;
import org.asiainfo.common.log.enums.BusinessType;
import org.springframework.validation.annotation.Validated;
import org.asiainfo.system.service.SysCategoryService;
import org.asiainfo.system.domain.vo.SysCategoryVo;
import org.asiainfo.system.domain.bo.SysCategoryBo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.asiainfo.common.core.domain.R;
import org.asiainfo.common.web.core.BaseController;
import jakarta.validation.constraints.*;
import org.asiainfo.common.core.validate.AddGroup;
import org.asiainfo.common.core.validate.EditGroup;

import java.util.List;

/**
 * 分类字典
 *
 * @author dotor-ww
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/category")
public class SysCategoryController extends BaseController {

    private final SysCategoryService sysCategoryService;

    /**
     * 分页查询
     *
     * @param sysCategoryBo 筛选条件
     * @return 查询结果
     */
    @SaCheckPermission("system:category:list")
    @GetMapping("/list")
    public R<Object> list(SysCategoryBo sysCategoryBo) {
        return R.ok(sysCategoryService.queryList(sysCategoryBo));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param categoryId 主键
     * @return 单条数据
     */
    @SaCheckPermission("system:category:query")
    @GetMapping("/{categoryId}")
    public R<SysCategoryVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable("categoryId") Long categoryId) {
        return R.ok(sysCategoryService.queryById(categoryId));
    }

    /**
     * 新增数据
     *
     * @param sysCategoryBo 实体
     * @return 新增结果
     */
    @SaCheckPermission("system:category:add")
    @Log(title = "分类字典配置", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysCategoryBo sysCategoryBo) {
        if (sysCategoryService.checkCategoryCodeUnique(sysCategoryBo)) {
            return R.fail("新增分类'" + sysCategoryBo.getCategoryName() + "'失败，分类code已存在");
        }
        sysCategoryService.insertByBo(sysCategoryBo);
        return R.ok();
    }

    /**
     * 编辑数据
     *
     * @param sysCategoryBo 实体
     * @return 编辑结果
     */
    @SaCheckPermission("system:category:edit")
    @Log(title = "分类字典配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysCategoryBo sysCategoryBo) {
        if (sysCategoryService.hasChildByCategoryId(sysCategoryBo.getCategoryId())) {
            throw new RuntimeException("分类字典下存在子字典,不能修改");
        }
        if (sysCategoryService.checkCategoryCodeUnique(sysCategoryBo)) {
            return R.fail("修改分类'" + sysCategoryBo.getCategoryName() + "'失败，分类code已存在");
        }
        sysCategoryService.updateByBo(sysCategoryBo);
        return R.ok();
    }

    /**
     * 删除数据
     *
     * @param categoryId 主键
     * @return 删除是否成功
     */
    @SaCheckPermission("system:category:remove")
    @Log(title = "分类字典配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{categoryId}")
    public R<Void> remove(@NotNull(message = "主键不能为空") @PathVariable Long categoryId) {
        if (sysCategoryService.hasChildByCategoryId(categoryId)) {
            return R.fail("分类字典下存在子字典,不能删除");
        }
        return toAjax(sysCategoryService.deleteWithValidById(categoryId));
    }

    /**
     * 获取分类字典树列表
     *
     * @param sysCategoryBo 实体
     * @return 树结构
     */
    @SaCheckPermission("system:category:query")
    @GetMapping("/getTreeSelect")
    public R<Object> getTreeSelect(SysCategoryBo sysCategoryBo) {
        List<SysCategoryVo> sysCategoryVos = sysCategoryService.selectCategoryList(sysCategoryBo);
        return R.ok(sysCategoryService.buildCategoryTreeSelect(sysCategoryVos));
    }
}

